<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>moduleloader_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_module_moduleloader_test.js.html">moduleloader_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Tests for goog.module.ModuleLoader.
<a name="line17"></a> * @author nicksantos@google.com (Nick Santos)
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.module.ModuleLoaderTest&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.array&#39;);
<a name="line23"></a>goog.require(&#39;goog.dom&#39;);
<a name="line24"></a>goog.require(&#39;goog.functions&#39;);
<a name="line25"></a>goog.require(&#39;goog.module.ModuleLoader&#39;);
<a name="line26"></a>goog.require(&#39;goog.module.ModuleManager&#39;);
<a name="line27"></a>goog.require(&#39;goog.module.ModuleManager.CallbackType&#39;);
<a name="line28"></a>goog.require(&#39;goog.object&#39;);
<a name="line29"></a>goog.require(&#39;goog.testing.AsyncTestCase&#39;);
<a name="line30"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line31"></a>goog.require(&#39;goog.testing.events.EventObserver&#39;);
<a name="line32"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line33"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line34"></a>goog.require(&#39;goog.userAgent.product&#39;);
<a name="line35"></a>
<a name="line36"></a>goog.setTestOnly(&#39;goog.module.ModuleLoaderTest&#39;);
<a name="line37"></a>
<a name="line38"></a>
<a name="line39"></a>var modA1Loaded = false;
<a name="line40"></a>var modA2Loaded = false;
<a name="line41"></a>var modB1Loaded = false;
<a name="line42"></a>
<a name="line43"></a>var moduleLoader = null;
<a name="line44"></a>var moduleManager = null;
<a name="line45"></a>var stubs = new goog.testing.PropertyReplacer();
<a name="line46"></a>
<a name="line47"></a>var testCase = goog.testing.AsyncTestCase.createAndInstall(document.title);
<a name="line48"></a>testCase.stepTimeout = 5 * 1000; // 5 seconds
<a name="line49"></a>
<a name="line50"></a>var EventType = goog.module.ModuleLoader.EventType;
<a name="line51"></a>var observer;
<a name="line52"></a>
<a name="line53"></a>testCase.setUp = function() {
<a name="line54"></a>  modA1Loaded = false;
<a name="line55"></a>  modA2Loaded = false;
<a name="line56"></a>  modB1Loaded = false;
<a name="line57"></a>
<a name="line58"></a>  goog.provide = goog.nullFunction;
<a name="line59"></a>  moduleManager = goog.module.ModuleManager.getInstance();
<a name="line60"></a>  stubs.replace(moduleManager, &#39;getBackOff_&#39;, goog.functions.constant(0));
<a name="line61"></a>
<a name="line62"></a>  moduleLoader = new goog.module.ModuleLoader();
<a name="line63"></a>  observer = new goog.testing.events.EventObserver();
<a name="line64"></a>
<a name="line65"></a>  goog.events.listen(
<a name="line66"></a>      moduleLoader, goog.object.getValues(EventType), observer);
<a name="line67"></a>
<a name="line68"></a>  moduleManager.setLoader(moduleLoader);
<a name="line69"></a>  moduleManager.setAllModuleInfo({
<a name="line70"></a>      &#39;modA&#39;: [],
<a name="line71"></a>      &#39;modB&#39;: [&#39;modA&#39;]
<a name="line72"></a>  });
<a name="line73"></a>  moduleManager.setModuleUris({
<a name="line74"></a>      &#39;modA&#39;: [&#39;testdata/modA_1.js&#39;, &#39;testdata/modA_2.js&#39;],
<a name="line75"></a>      &#39;modB&#39;: [&#39;testdata/modB_1.js&#39;]
<a name="line76"></a>  });
<a name="line77"></a>
<a name="line78"></a>  assertNotLoaded(&#39;modA&#39;);
<a name="line79"></a>  assertNotLoaded(&#39;modB&#39;);
<a name="line80"></a>  assertFalse(modA1Loaded);
<a name="line81"></a>};
<a name="line82"></a>
<a name="line83"></a>testCase.tearDown = function() {
<a name="line84"></a>  stubs.reset();
<a name="line85"></a>
<a name="line86"></a>  // Ensure that the module manager was created.
<a name="line87"></a>  assertNotNull(goog.module.ModuleManager.getInstance());
<a name="line88"></a>  moduleManager = goog.module.ModuleManager.instance_ = null;
<a name="line89"></a>
<a name="line90"></a>  // tear down the module loaded flag.
<a name="line91"></a>  modA1Loaded = false;
<a name="line92"></a>
<a name="line93"></a>  // Remove all the fake scripts.
<a name="line94"></a>  var scripts = goog.array.clone(
<a name="line95"></a>      document.getElementsByTagName(&#39;SCRIPT&#39;));
<a name="line96"></a>  for (var i = 0; i &lt; scripts.length; i++) {
<a name="line97"></a>    if (scripts[i].src.indexOf(&#39;testdata&#39;) != -1) {
<a name="line98"></a>      goog.dom.removeNode(scripts[i]);
<a name="line99"></a>    }
<a name="line100"></a>  }
<a name="line101"></a>};
<a name="line102"></a>
<a name="line103"></a>function testLoadModuleA() {
<a name="line104"></a>  testCase.waitForAsync(&#39;wait for module A load&#39;);
<a name="line105"></a>  moduleManager.execOnLoad(&#39;modA&#39;, function() {
<a name="line106"></a>    testCase.continueTesting();
<a name="line107"></a>    assertLoaded(&#39;modA&#39;);
<a name="line108"></a>    assertNotLoaded(&#39;modB&#39;);
<a name="line109"></a>    assertTrue(modA1Loaded);
<a name="line110"></a>
<a name="line111"></a>    assertEquals(&#39;EVALUATE_CODE&#39;,
<a name="line112"></a>        0, observer.getEvents(EventType.EVALUATE_CODE).length);
<a name="line113"></a>    assertEquals(&#39;REQUEST_SUCCESS&#39;,
<a name="line114"></a>        1, observer.getEvents(EventType.REQUEST_SUCCESS).length);
<a name="line115"></a>    assertArrayEquals(
<a name="line116"></a>        [&#39;modA&#39;], observer.getEvents(EventType.REQUEST_SUCCESS)[0].moduleIds);
<a name="line117"></a>    assertEquals(&#39;REQUEST_ERROR&#39;,
<a name="line118"></a>        0, observer.getEvents(EventType.REQUEST_ERROR).length);
<a name="line119"></a>  });
<a name="line120"></a>}
<a name="line121"></a>
<a name="line122"></a>function testLoadModuleB() {
<a name="line123"></a>  testCase.waitForAsync(&#39;wait for module B load&#39;);
<a name="line124"></a>  moduleManager.execOnLoad(&#39;modB&#39;, function() {
<a name="line125"></a>    testCase.continueTesting();
<a name="line126"></a>    assertLoaded(&#39;modA&#39;);
<a name="line127"></a>    assertLoaded(&#39;modB&#39;);
<a name="line128"></a>    assertTrue(modA1Loaded);
<a name="line129"></a>  });
<a name="line130"></a>}
<a name="line131"></a>
<a name="line132"></a>function testLoadDebugModuleA() {
<a name="line133"></a>  testCase.waitForAsync(&#39;wait for module A load&#39;);
<a name="line134"></a>  moduleLoader.setDebugMode(true);
<a name="line135"></a>  moduleManager.execOnLoad(&#39;modA&#39;, function() {
<a name="line136"></a>    testCase.continueTesting();
<a name="line137"></a>    assertLoaded(&#39;modA&#39;);
<a name="line138"></a>    assertNotLoaded(&#39;modB&#39;);
<a name="line139"></a>    assertTrue(modA1Loaded);
<a name="line140"></a>  });
<a name="line141"></a>}
<a name="line142"></a>
<a name="line143"></a>function testLoadDebugModuleB() {
<a name="line144"></a>  testCase.waitForAsync(&#39;wait for module B load&#39;);
<a name="line145"></a>  moduleLoader.setDebugMode(true);
<a name="line146"></a>  moduleManager.execOnLoad(&#39;modB&#39;, function() {
<a name="line147"></a>    testCase.continueTesting();
<a name="line148"></a>    assertLoaded(&#39;modA&#39;);
<a name="line149"></a>    assertLoaded(&#39;modB&#39;);
<a name="line150"></a>    assertTrue(modA1Loaded);
<a name="line151"></a>  });
<a name="line152"></a>}
<a name="line153"></a>
<a name="line154"></a>function testLoadDebugModuleAThenB() {
<a name="line155"></a>  // Swap the script tags of module A, to introduce a race condition.
<a name="line156"></a>  // See the comments on this in ModuleLoader&#39;s debug loader.
<a name="line157"></a>  moduleManager.setModuleUris({
<a name="line158"></a>      &#39;modA&#39;: [&#39;testdata/modA_2.js&#39;, &#39;testdata/modA_1.js&#39;],
<a name="line159"></a>      &#39;modB&#39;: [&#39;testdata/modB_1.js&#39;]
<a name="line160"></a>  });
<a name="line161"></a>  testCase.waitForAsync(&#39;wait for module B load&#39;);
<a name="line162"></a>  moduleLoader.setDebugMode(true);
<a name="line163"></a>  moduleManager.execOnLoad(&#39;modB&#39;, function() {
<a name="line164"></a>    testCase.continueTesting();
<a name="line165"></a>    assertLoaded(&#39;modA&#39;);
<a name="line166"></a>    assertLoaded(&#39;modB&#39;);
<a name="line167"></a>
<a name="line168"></a>    var scripts = goog.array.clone(
<a name="line169"></a>        document.getElementsByTagName(&#39;SCRIPT&#39;));
<a name="line170"></a>    var seenLastScriptOfModuleA = false;
<a name="line171"></a>    for (var i = 0; i &lt; scripts.length; i++) {
<a name="line172"></a>      var uri = scripts[i].src;
<a name="line173"></a>      if (uri.indexOf(&#39;modA_1.js&#39;) &gt;= 0) {
<a name="line174"></a>        seenLastScriptOfModuleA = true;
<a name="line175"></a>      } else if (uri.indexOf(&#39;modB&#39;) &gt;= 0) {
<a name="line176"></a>        assertTrue(seenLastScriptOfModuleA);
<a name="line177"></a>      }
<a name="line178"></a>    }
<a name="line179"></a>  });
<a name="line180"></a>}
<a name="line181"></a>
<a name="line182"></a>function testSourceInjection() {
<a name="line183"></a>  moduleLoader.setSourceUrlInjection(true);
<a name="line184"></a>  assertSourceInjection();
<a name="line185"></a>}
<a name="line186"></a>
<a name="line187"></a>function testSourceInjectionViaDebugMode() {
<a name="line188"></a>  moduleLoader.setDebugMode(true);
<a name="line189"></a>  assertSourceInjection();
<a name="line190"></a>}
<a name="line191"></a>
<a name="line192"></a>function assertSourceInjection() {
<a name="line193"></a>  testCase.waitForAsync(&#39;wait for module B load&#39;);
<a name="line194"></a>
<a name="line195"></a>  moduleManager.execOnLoad(&#39;modB&#39;, function() {
<a name="line196"></a>    testCase.continueTesting();
<a name="line197"></a>
<a name="line198"></a>    assertTrue(!!throwErrorInModuleB);
<a name="line199"></a>
<a name="line200"></a>    var ex = assertThrows(function() {
<a name="line201"></a>      throwErrorInModuleB();
<a name="line202"></a>    });
<a name="line203"></a>
<a name="line204"></a>    var stackTrace = ex.stack.toString();
<a name="line205"></a>    var expectedString = &#39;testdata/modB_1.js&#39;;
<a name="line206"></a>
<a name="line207"></a>    if (goog.module.ModuleLoader.supportsSourceUrlStackTraces()) {
<a name="line208"></a>      // Source URL should be added in eval or in jsloader.
<a name="line209"></a>      assertContains(expectedString, stackTrace);
<a name="line210"></a>    } else if (moduleLoader.isDebugMode()) {
<a name="line211"></a>      // Browsers used jsloader, thus URLs are present.
<a name="line212"></a>      assertContains(expectedString, stackTrace);
<a name="line213"></a>    } else {
<a name="line214"></a>      // Browser used eval, does not support source URL.
<a name="line215"></a>      assertNotContains(expectedString, stackTrace);
<a name="line216"></a>    }
<a name="line217"></a>  });
<a name="line218"></a>}
<a name="line219"></a>
<a name="line220"></a>function testModuleLoaderRecursesTooDeep(opt_numModules) {
<a name="line221"></a>  // There was a bug in the module loader where it would retry recursively
<a name="line222"></a>  // whenever there was a synchronous failure in the module load. When you
<a name="line223"></a>  // asked for modB, it would try to load its dependency modA. When modA
<a name="line224"></a>  // failed, it would move onto modB, and then start over, repeating until it
<a name="line225"></a>  // ran out of stack.
<a name="line226"></a>  var numModules = opt_numModules || 1;
<a name="line227"></a>  var uris = {};
<a name="line228"></a>  var deps = {};
<a name="line229"></a>  var mods = [];
<a name="line230"></a>  for (var num = 0; num &lt; numModules; num++) {
<a name="line231"></a>    var modName = &#39;mod&#39; + num;
<a name="line232"></a>    mods.unshift(modName);
<a name="line233"></a>    uris[modName] = [];
<a name="line234"></a>    deps[modName] = num ? [&#39;mod&#39; + (num - 1)] : [];
<a name="line235"></a>    for (var i = 0; i &lt; 5; i++) {
<a name="line236"></a>      uris[modName].push(
<a name="line237"></a>          &#39;http://www.google.com/crossdomain&#39; + num + &#39;x&#39; + i + &#39;.js&#39;);
<a name="line238"></a>    }
<a name="line239"></a>  }
<a name="line240"></a>
<a name="line241"></a>  moduleManager.setAllModuleInfo(deps);
<a name="line242"></a>  moduleManager.setModuleUris(uris);
<a name="line243"></a>
<a name="line244"></a>  // Make all XHRs throw an error, so that we test the error-handling
<a name="line245"></a>  // functionality.
<a name="line246"></a>  var oldXmlHttp = goog.net.XmlHttp;
<a name="line247"></a>  stubs.set(goog.net, &#39;XmlHttp&#39;, function() {
<a name="line248"></a>    return {
<a name="line249"></a>       open: goog.functions.error(&#39;mock error&#39;),
<a name="line250"></a>       abort: goog.nullFunction
<a name="line251"></a>    };
<a name="line252"></a>  });
<a name="line253"></a>  goog.object.extend(goog.net.XmlHttp, oldXmlHttp);
<a name="line254"></a>
<a name="line255"></a>  var errorCount = 0;
<a name="line256"></a>  var errorIds = [];
<a name="line257"></a>  var errorHandler = function(ignored, modId) {
<a name="line258"></a>    errorCount++;
<a name="line259"></a>    errorIds.push(modId);
<a name="line260"></a>  };
<a name="line261"></a>  moduleManager.registerCallback(
<a name="line262"></a>      goog.module.ModuleManager.CallbackType.ERROR,
<a name="line263"></a>      errorHandler);
<a name="line264"></a>
<a name="line265"></a>  moduleManager.execOnLoad(mods[0], function() {
<a name="line266"></a>    fail(&#39;modB should not load successfully&#39;);
<a name="line267"></a>  });
<a name="line268"></a>
<a name="line269"></a>  assertEquals(mods.length, errorCount);
<a name="line270"></a>
<a name="line271"></a>  goog.array.sort(mods);
<a name="line272"></a>  goog.array.sort(errorIds);
<a name="line273"></a>  assertArrayEquals(mods, errorIds);
<a name="line274"></a>
<a name="line275"></a>  assertArrayEquals([], moduleManager.requestedModuleIdsQueue_);
<a name="line276"></a>  assertArrayEquals([], moduleManager.userInitiatedLoadingModuleIds_);
<a name="line277"></a>}
<a name="line278"></a>
<a name="line279"></a>function testModuleLoaderRecursesTooDeep2modules() {
<a name="line280"></a>  testModuleLoaderRecursesTooDeep(2);
<a name="line281"></a>}
<a name="line282"></a>
<a name="line283"></a>function testModuleLoaderRecursesTooDeep3modules() {
<a name="line284"></a>  testModuleLoaderRecursesTooDeep(3);
<a name="line285"></a>}
<a name="line286"></a>
<a name="line287"></a>function testModuleLoaderRecursesTooDeep4modules() {
<a name="line288"></a>  testModuleLoaderRecursesTooDeep(3);
<a name="line289"></a>}
<a name="line290"></a>
<a name="line291"></a>function testErrback() {
<a name="line292"></a>  // Don&#39;t run this test on IE, because the way the test runner catches
<a name="line293"></a>  // errors on IE plays badly with the simulated errors in the test.
<a name="line294"></a>  if (goog.userAgent.IE) return;
<a name="line295"></a>
<a name="line296"></a>  // Modules will throw an exception if this boolean is set to true.
<a name="line297"></a>  modA1Loaded = true;
<a name="line298"></a>
<a name="line299"></a>  var errorHandler = function() {
<a name="line300"></a>    testCase.continueTesting();
<a name="line301"></a>    assertNotLoaded(&#39;modA&#39;);
<a name="line302"></a>  };
<a name="line303"></a>  moduleManager.registerCallback(
<a name="line304"></a>      goog.module.ModuleManager.CallbackType.ERROR,
<a name="line305"></a>      errorHandler);
<a name="line306"></a>
<a name="line307"></a>  moduleManager.execOnLoad(&#39;modA&#39;, function() {
<a name="line308"></a>    fail(&#39;modA should not load successfully&#39;);
<a name="line309"></a>  });
<a name="line310"></a>
<a name="line311"></a>  testCase.waitForAsync(&#39;wait for the error callback&#39;);
<a name="line312"></a>}
<a name="line313"></a>
<a name="line314"></a>function testPrefetchThenLoadModuleA() {
<a name="line315"></a>  moduleManager.prefetchModule(&#39;modA&#39;);
<a name="line316"></a>  stubs.set(goog.net.BulkLoader.prototype, &#39;load&#39;, function() {
<a name="line317"></a>    fail(&#39;modA should not be reloaded&#39;)
<a name="line318"></a>  });
<a name="line319"></a>
<a name="line320"></a>  testCase.waitForAsync(&#39;wait for module A load&#39;);
<a name="line321"></a>  moduleManager.execOnLoad(&#39;modA&#39;, function() {
<a name="line322"></a>    testCase.continueTesting();
<a name="line323"></a>    assertLoaded(&#39;modA&#39;);
<a name="line324"></a>    assertEquals(&#39;REQUEST_SUCCESS&#39;,
<a name="line325"></a>        1, observer.getEvents(EventType.REQUEST_SUCCESS).length);
<a name="line326"></a>    assertArrayEquals(
<a name="line327"></a>        [&#39;modA&#39;], observer.getEvents(EventType.REQUEST_SUCCESS)[0].moduleIds);
<a name="line328"></a>    assertEquals(&#39;REQUEST_ERROR&#39;,
<a name="line329"></a>        0, observer.getEvents(EventType.REQUEST_ERROR).length);
<a name="line330"></a>  });
<a name="line331"></a>}
<a name="line332"></a>
<a name="line333"></a>function testPrefetchThenLoadModuleB() {
<a name="line334"></a>  moduleManager.prefetchModule(&#39;modB&#39;);
<a name="line335"></a>  stubs.set(goog.net.BulkLoader.prototype, &#39;load&#39;, function() {
<a name="line336"></a>    fail(&#39;modA and modB should not be reloaded&#39;)
<a name="line337"></a>  });
<a name="line338"></a>
<a name="line339"></a>  testCase.waitForAsync(&#39;wait for module B load&#39;);
<a name="line340"></a>  moduleManager.execOnLoad(&#39;modB&#39;, function() {
<a name="line341"></a>    testCase.continueTesting();
<a name="line342"></a>    assertLoaded(&#39;modA&#39;);
<a name="line343"></a>    assertLoaded(&#39;modB&#39;);
<a name="line344"></a>    assertEquals(&#39;REQUEST_SUCCESS&#39;,
<a name="line345"></a>        2, observer.getEvents(EventType.REQUEST_SUCCESS).length);
<a name="line346"></a>    assertArrayEquals(
<a name="line347"></a>        [&#39;modA&#39;], observer.getEvents(EventType.REQUEST_SUCCESS)[0].moduleIds);
<a name="line348"></a>    assertArrayEquals(
<a name="line349"></a>        [&#39;modB&#39;], observer.getEvents(EventType.REQUEST_SUCCESS)[1].moduleIds);
<a name="line350"></a>    assertEquals(&#39;REQUEST_ERROR&#39;,
<a name="line351"></a>        0, observer.getEvents(EventType.REQUEST_ERROR).length);
<a name="line352"></a>  });
<a name="line353"></a>}
<a name="line354"></a>
<a name="line355"></a>function testPrefetchModuleAThenLoadModuleB() {
<a name="line356"></a>  moduleManager.prefetchModule(&#39;modA&#39;);
<a name="line357"></a>
<a name="line358"></a>  testCase.waitForAsync(&#39;wait for module A load&#39;);
<a name="line359"></a>  moduleManager.execOnLoad(&#39;modB&#39;, function() {
<a name="line360"></a>    testCase.continueTesting();
<a name="line361"></a>    assertLoaded(&#39;modA&#39;);
<a name="line362"></a>    assertLoaded(&#39;modB&#39;);
<a name="line363"></a>    assertEquals(&#39;REQUEST_SUCCESS&#39;,
<a name="line364"></a>        2, observer.getEvents(EventType.REQUEST_SUCCESS).length);
<a name="line365"></a>    assertArrayEquals(
<a name="line366"></a>        [&#39;modA&#39;], observer.getEvents(EventType.REQUEST_SUCCESS)[0].moduleIds);
<a name="line367"></a>    assertArrayEquals(
<a name="line368"></a>        [&#39;modB&#39;], observer.getEvents(EventType.REQUEST_SUCCESS)[1].moduleIds);
<a name="line369"></a>    assertEquals(&#39;REQUEST_ERROR&#39;,
<a name="line370"></a>        0, observer.getEvents(EventType.REQUEST_ERROR).length);
<a name="line371"></a>  });
<a name="line372"></a>}
<a name="line373"></a>
<a name="line374"></a>function testLoadModuleBThenPrefetchModuleA() {
<a name="line375"></a>  testCase.waitForAsync(&#39;wait for module A load&#39;);
<a name="line376"></a>  moduleManager.execOnLoad(&#39;modB&#39;, function() {
<a name="line377"></a>    testCase.continueTesting();
<a name="line378"></a>    assertLoaded(&#39;modA&#39;);
<a name="line379"></a>    assertLoaded(&#39;modB&#39;);
<a name="line380"></a>    assertEquals(&#39;REQUEST_SUCCESS&#39;,
<a name="line381"></a>        2, observer.getEvents(EventType.REQUEST_SUCCESS).length);
<a name="line382"></a>    assertArrayEquals(
<a name="line383"></a>        [&#39;modA&#39;], observer.getEvents(EventType.REQUEST_SUCCESS)[0].moduleIds);
<a name="line384"></a>    assertArrayEquals(
<a name="line385"></a>        [&#39;modB&#39;], observer.getEvents(EventType.REQUEST_SUCCESS)[1].moduleIds);
<a name="line386"></a>    assertEquals(&#39;REQUEST_ERROR&#39;,
<a name="line387"></a>        0, observer.getEvents(EventType.REQUEST_ERROR).length);
<a name="line388"></a>    assertThrows(&#39;Module load already requested: modB&#39;,
<a name="line389"></a>        function() {
<a name="line390"></a>          moduleManager.prefetchModule(&#39;modA&#39;)
<a name="line391"></a>        });
<a name="line392"></a>  });
<a name="line393"></a>}
<a name="line394"></a>
<a name="line395"></a>function testPrefetchModuleWithBatchModeEnabled() {
<a name="line396"></a>  moduleManager.setBatchModeEnabled(true);
<a name="line397"></a>  assertThrows(&#39;Modules prefetching is not supported in batch mode&#39;,
<a name="line398"></a>      function() {
<a name="line399"></a>        moduleManager.prefetchModule(&#39;modA&#39;);
<a name="line400"></a>      });
<a name="line401"></a>}
<a name="line402"></a>
<a name="line403"></a>function testLoadErrorCallbackExecutedWhenPrefetchFails() {
<a name="line404"></a>  // Make all XHRs throw an error, so that we test the error-handling
<a name="line405"></a>  // functionality.
<a name="line406"></a>  var oldXmlHttp = goog.net.XmlHttp;
<a name="line407"></a>  stubs.set(goog.net, &#39;XmlHttp&#39;, function() {
<a name="line408"></a>    return {
<a name="line409"></a>       open: goog.functions.error(&#39;mock error&#39;),
<a name="line410"></a>       abort: goog.nullFunction
<a name="line411"></a>    };
<a name="line412"></a>  });
<a name="line413"></a>  goog.object.extend(goog.net.XmlHttp, oldXmlHttp);
<a name="line414"></a>
<a name="line415"></a>  var errorCount = 0;
<a name="line416"></a>  var errorHandler = function() {
<a name="line417"></a>    errorCount++;
<a name="line418"></a>  };
<a name="line419"></a>  moduleManager.registerCallback(
<a name="line420"></a>      goog.module.ModuleManager.CallbackType.ERROR,
<a name="line421"></a>      errorHandler);
<a name="line422"></a>
<a name="line423"></a>  moduleLoader.prefetchModule(&#39;modA&#39;, moduleManager.moduleInfoMap_[&#39;modA&#39;]);
<a name="line424"></a>  moduleLoader.loadModules([&#39;modA&#39;], moduleManager.moduleInfoMap_,
<a name="line425"></a>    function() {
<a name="line426"></a>      fail(&#39;modA should not load successfully&#39;)
<a name="line427"></a>    }, errorHandler);
<a name="line428"></a>
<a name="line429"></a>  assertEquals(1, errorCount);
<a name="line430"></a>}
<a name="line431"></a>
<a name="line432"></a>function assertLoaded(id) {
<a name="line433"></a>  assertTrue(moduleManager.getModuleInfo(id).isLoaded());
<a name="line434"></a>}
<a name="line435"></a>
<a name="line436"></a>function assertNotLoaded(id) {
<a name="line437"></a>  assertFalse(moduleManager.getModuleInfo(id).isLoaded());
<a name="line438"></a>}
</pre>


</body>
</html>
